package edu.mama.college.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.Part;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

@Component
@Slf4j
public class FileUtil {


    private static String fileLocation;

    private static String imagePath;

    private static String videoPath;

    @Value("${upload.location}")
    private  String fileLocationInfo;

    @Value("${upload.image}")
    private  String imagePathInfo;

    @Value("${upload.video}")
    private  String videoPathInfo;

    // 将配置文件赋值给静态变量
    @PostConstruct
    public void getConf() {
        fileLocation = this.fileLocationInfo;
        imagePath = this.imagePathInfo;
        videoPath = this.videoPathInfo;
    }


    public static Map<String,String> uploadFile(Part part, String type) throws Exception {
        // 读取文件
        String fileName = UUID.randomUUID().toString();
        InputStream inputStream;
        try{
            inputStream = part.getInputStream();
        }catch(IOException e){
            log.error("上传文件失败：",e);
            throw new Exception("上传文件失败");
        }

        // 输出文件地址
        String filePath;
        // 数据库文件地址
        String url;
        switch (type){
            case "video":
                filePath = fileLocation+videoPath;
                url = videoPath+fileName;
                break;
            case "image":
                filePath = fileLocation+imagePath;
                url = imagePath+fileName;
                break;
            default:
                throw new Exception("文件类型错误");
        }


        // 创建文件夹
        File pFile = new File(filePath);
        if(!pFile.exists()){
            pFile.mkdirs();
        }
        File file = new File(pFile,fileName);


        // 上传文件
        OutputStream out;
        // 1k缓冲流
        byte[] bytes = new byte[1024];
        // 读取到的数据长度
        int len;
        try{
           out = new FileOutputStream(file);
           // 读取
           while ((len = inputStream.read(bytes)) != -1){
               out.write(bytes, 0,len);
           }
        } catch(IOException e){
            log.error("上传失败",e);
            throw new Exception("上传失败");
        }

        try {
            part.write(fileName);
        }catch(IOException e){
            log.error("上传失败",e);
            throw new Exception("上传失败");
        }

        // 返回结果
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("name",part.getSubmittedFileName());
        resultMap.put("url",url);
        resultMap.put("size",String.valueOf(part.getSize()));

        return resultMap;
    }

    public static void deleteFile(String url) throws Exception {
        String filePath = fileLocation+url;
        File file = new File(filePath);
        if(file.exists()){
            try {
                file.delete();
            }catch(Exception e){
                log.error("删除文件失败：",e);
                throw new Exception("删除文件失败");
            }
        }
    }
}
